import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select

class Base():
    # 参数化
    def __init__(self,driver):
        self.driver = driver

    # 打开网址
    def open_url(self,url):
        self.driver.get(url)

    # 定位+显示等待+异常截图
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as p:
            # 时间戳
            aa = time.strftime('%Y%m%d%H%M%S',time.localtime())
            # 截图
            self.driver.save_screenshot(aa+'.png')
            print(p)

    # 点击
    def click(self,loc):
        self.find_element(loc).click()

    # 输入文本
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    # 隐式等待
    def implicitly_wait(self):
        self.driver.implicitly_wait(10)

    # 获取文本
    def text(self,loc):
        return self.find_element(loc).text

    # 滚动条（js注入）
    def scrollTo(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')

    # alert弹窗处理
    # 确定
    def accept(self):
        aa = self.driver.switch_to.alert
        aa.accept()

    # 取消
    def dismiss(self):
        aa = self.driver.switch_to.alert
        aa.dismiss()

    # iframe切换
    # 进入
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    # 退出
    def content(self,loc):
        self.driver.switch_to.default_content()

    # 窗口切换
    def handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # 鼠标事件
    # 双击
    def double_click(self,loc):
        ActionChains(self.find_element(loc)).double_click().perform()
    # 右键
    def context_click(self,loc):
        ActionChains(self.find_element(loc)).context_click().perform()
    # 长按
    def click_and_hold(self,loc):
        ActionChains(self.find_element(loc)).click_and_hold().perform()
    # 键盘事件
    # 回车
    def enter(self):
        self.driver.find_element().send_keys(Keys.ENTER)

    # 保存
    def control_s(self):
        self.driver.find_element().send_keys(Keys.CONTROL,'s')
    # 剪切
    def control_x(self):
        self.driver.find_element().send_keys(Keys.CONTROL,'x')
    # 复制
    def control_c(self):
        self.driver.find_element().send_keys(Keys.CONTROL,'c')
    # 粘贴
    def control_v(self):
        self.driver.find_element().send_keys(Keys.CONTROL,'v')
    # 浏览器窗口操作
    def refresh(self):
        self.driver.refresh()
    def close(self):
        self.driver.close()
    def back(self):
        self.driver.back()
    # 下拉框选择
    def index(self,loc,x):
        aa = Select(self.driver.find_element(loc))
        aa.select_by_index(x)
    def visible_text(self,loc,x):
        aa = Select(self.driver.find_element(loc))
        aa.deselect_by_visible_text(x)
    def value(self,loc,x):
        aa = Select(self.driver.find_element(loc))
        aa.deselect_by_value(x)

